unit F_PrinterStatistics;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, DB, ZAbstractRODataset, ZDataset, StdCtrls, Mask,
  JvExMask, JvToolEdit, cxControls, cxContainer, cxCheckListBox,
  cxGraphics, cxEdit, cxTextEdit, cxMaskEdit, cxDropDownEdit,
  cxImageComboBox, ImgList;

type
  TFPrinters = class(TForm)
    Z_Get_Printers_Count: TZReadOnlyQuery;
    Z_Get_Total_Count: TZReadOnlyQuery;
    Panel2: TPanel;
    L_Fecha_Inicial: TLabel;
    DTP_Fecha_Inicial: TJvDateEdit;
    Label1: TLabel;
    DTP_Fecha_Final: TJvDateEdit;
    Panel1: TScrollBox;
    ImageList1: TImageList;
    Label2: TLabel;
    cxImageComboBox1: TcxImageComboBox;
    CB_Order: TCheckBox;
    procedure DTP_Fecha_InicialKeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure AddPrinter(PrinterName:String; PrinterIndex: Integer; TotalPages, Total: integer);

    procedure GetPrinters();
    procedure GetTotalCount();

    procedure GeneratePanels();

    procedure ClearPanel();
  end;

implementation

uses printers, FR_PrinterStatistic, DM_Persistencia;

{$R *.dfm}

procedure TFPrinters.GetTotalCount();
begin
  with Z_Get_Total_Count do
    begin
      Close;
      ParamByName('FECHA_INICIAL').AsDate:= StrToDate(FormatDateTime('dd/MM/yyyy', DTP_Fecha_Inicial.Date));
      ParamByName('FECHA_FINAL').AsDate:= StrToDate(FormatDateTime('dd/MM/yyyy', DTP_Fecha_Final.Date));
      Open;
    end;
end;

procedure TFPrinters.GetPrinters();
begin
  with Z_Get_Printers_Count do
    begin
      Close;
      SQL.Text:=  'SELECT :SOURCE AS SOURCE, SUM (TOTAL_PAGES) AS TOTAL_PAGES'+
                  ' FROM LOG_IMPRESIONES'+
                  ' WHERE JOB_STATUS = 6'+
                  ' AND CAST (FECHA_HORA AS DATE) BETWEEN :FECHA_INICIAL AND :FECHA_FINAL'+
                  ' GROUP BY :SOURCE'+
                  ' HAVING SUM (TOTAL_PAGES) > 0;';
      SQL.Text:= StringReplace(SQL.Text, ':SOURCE', VarToStr(cxImageComboBox1.Properties.Items.Items[cxImageComboBox1.ItemIndex].Value), [rfReplaceAll, rfIgnoreCase]);
      ParamByName('FECHA_INICIAL').AsDate:= StrToDate(FormatDateTime('dd/MM/yyyy', DTP_Fecha_Inicial.Date));
      ParamByName('FECHA_FINAL').AsDate:= StrToDate(FormatDateTime('dd/MM/yyyy', DTP_Fecha_Final.Date));
      Open;
    end;
end;

procedure TFPrinters.GeneratePanels();
var
  i:integer;
begin
    ClearPanel();

    if (not Z_Get_Total_Count.Active) then
      GetTotalCount();

    GetPrinters();

    with Z_Get_Printers_Count do
      begin
        i:= 0;
        if (not CB_Order.Checked) then
          SortType:= stDescending
        else
          SortType:= stAscending;

        while not Eof do
          begin
            AddPrinter(FieldByName('SOURCE').AsString , i, FieldByName('TOTAL_PAGES').AsInteger, Z_Get_Total_Count.FieldByName('TOTAL').AsInteger);
            Next;
            inc(i);
          end;
      end;
end;

procedure TFPrinters.ClearPanel();
var i:integer;
begin
  for i:=0 to Panel1.ControlCount-1 do
    if (Panel1.Controls[0] is TFR_Printer) then
      TFR_Printer(Panel1.Controls[0]).Free;
  Panel1.Refresh;
end;


procedure TFPrinters.AddPrinter(PrinterName:String; PrinterIndex: Integer; TotalPages, Total: integer);
var
  FR_Printer: TFR_Printer;
begin
  FR_Printer:= TFR_Printer.Create(Panel1, PrinterName, TotalPages, Total);
  FR_Printer.Parent:= Panel1;
  FR_Printer.Name:= 'Frame' + IntToStr(PrinterIndex);
  FR_Printer.Top:= (FR_Printer.Height)*(PrinterIndex);
  FR_Printer.Show;
end;

procedure TFPrinters.DTP_Fecha_InicialKeyPress(Sender: TObject;
  var Key: Char);
begin
  if (Key = Chr(13)) then
    GeneratePanels();
end;

end.
